﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using webapp4U;
using webapp4U.UI;
using webapp4U.BOL;
using System.Data;

public partial class Usercontrol_ShoppingCart : System.Web.UI.UserControl , ICallbackEventHandler
{
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
       
        List<ShoppingCart> ListShopCart = new List<ShoppingCart>();
        if (Session["ShoppingCart"] != null)
        {
            ListShopCart = (List<ShoppingCart>)Session["ShoppingCart"];
            gv_ShoppingCart.DataSource = ListShopCart;
            gv_ShoppingCart.DataBind();
            lblTotal.Text = String.Format("{0:###,###}", ShoppingCart.GetTotalShoppingCart(ListShopCart).ToString());
        }
        else
            lblTotal.Text = "0";
        
       
    }
    protected Double GetTotalShoppingCart()
    {
        double total = 0;
        List<ShoppingCart> ListShopCart = new List<ShoppingCart>();
        if (Session["ShoppingCart"] != null)
        {
            ListShopCart = (List<ShoppingCart>)Session["ShoppingCart"];
            total = ShoppingCart.GetTotalShoppingCart(ListShopCart);
        }

        return total;
    }

    public void BindData(List<ShoppingCart> ListShopCart)
    {
        gv_ShoppingCart.DataSource = ListShopCart;
        gv_ShoppingCart.DataBind();
        lblTotal.Text = String.Format("{0:###,###}", ShoppingCart.GetTotalShoppingCart(ListShopCart).ToString());
    }
    private string _callbackArg;
    void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
    {
        _callbackArg = eventArgument;
    }
    
    string ICallbackEventHandler.GetCallbackResult()
    {
        return RaiseCallbackEvent(_callbackArg);
    }    
    protected virtual string RaiseCallbackEvent(string eventArgument)
    {        
        string error = "";
        List<ShoppingCart> ListShopCart = new List<ShoppingCart>();
        string itemkey = eventArgument;
        bool flagProducOrder = false;
        if (eventArgument.Length > 15)
        {
            if (eventArgument.Substring(0, 15) == "btnProductOrder")
            {
                itemkey = eventArgument.Substring(15);
                flagProducOrder = true;
            }
        }
        //else
        //{ 
        
        //}

        Products obj = Products.GetProductsByItemKey(itemkey, ref error);
        ShoppingCart shop = new ShoppingCart();
        shop.AddedDate = DateTime.Now;
        shop.ItemKey = obj.ItemKey;
        shop.Description = obj.Description;
        shop.UnitPrice = obj.UnitPrice;
        shop.Quantity = 1;
        shop.Subtotal = obj.UnitPrice;

            
        if (Session["ShoppingCart"] == null)
        {
            ListShopCart.Add(shop);
            Session["ShoppingCart"] = ListShopCart;
        }
        else
        {
            ListShopCart = (List<ShoppingCart>)Session["ShoppingCart"];
            Session["ShoppingCart"] = ShoppingCart.AddItemToShoppingCart(ListShopCart, shop);
        }
         
        //if (flagProducOrder)
        //{
        //    Response.Redirect(Globals.ApplicationPath + "Page/Products/shoppingcart.aspx?Action=DetailCart");
        //}

        BindData(ListShopCart);
        return RenderControl(pnSoppingCart);         
       

    }
    public string RenderControl(Control ctrl)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter tw = new StringWriter(sb);
        HtmlTextWriter hw = new HtmlTextWriter(tw);

        ctrl.RenderControl(hw);
        return sb.ToString();
    }

    protected override void Render(HtmlTextWriter writer)
    {
        EnsureChildControls();
        base.Render(writer);
    }


    protected void Page_PreRender(object sender, EventArgs e)
    {

        if (Session["ShoppingCart"] != null)
        {
            List<ShoppingCart> ListShopCart = new List<ShoppingCart>();
            ListShopCart = (List<ShoppingCart>)Session["ShoppingCart"];
            gv_ShoppingCart.DataSource = ListShopCart;
            gv_ShoppingCart.DataBind();
            lblTotal.Text = String.Format("{0:###,###}", ShoppingCart.GetTotalShoppingCart(ListShopCart).ToString());
        }
        else
            lblTotal.Text = "0";
        //Page_Load(sender, e);
    }

    protected Double GetTotalShoppingCartTemp()
    {
        double total = 0;
        List<ShoppingCart> ListShopCart = new List<ShoppingCart>();
        if (Session["ShoppingCartTemp"] != null)
        {
            ListShopCart = (List<ShoppingCart>)Session["ShoppingCartTemp"];
            total = ShoppingCart.GetTotalShoppingCart(ListShopCart);
        }

        return total;
    }
    protected void btnContinue_Click(object sender, EventArgs e)
    {
        //lbResulFindShoppingCart.Text = txtPhoneOrIDNo.Text;
        //lbResulFindShoppingCart.Visible = true;

        DataTable dtCustomer = new DataTable();
        dtCustomer = ShoppingCart.FindCustomerByPhoneOrIdNo(ref error, txtPhoneOrIDNo.Text);
        if (dtCustomer.Rows.Count > 0)
        {
            Session["CustomerFind"] = dtCustomer;
            DataTable dtOrderTemp = new DataTable();
            dtOrderTemp = ShoppingCart.GetOrderTempByCustomerKey(ref error, dtCustomer.Rows[0]["customer_key"].ToString());
            if (dtOrderTemp.Rows.Count > 0)
            {
                Session["OrderTemp"] = dtOrderTemp;
                List<ShoppingCart> ListShopCartTemp = new List<ShoppingCart>();
                ListShopCartTemp = ShoppingCart.DetailOrderTemp(dtOrderTemp.Rows[0]["tranno"].ToString(), ref error);
                if (ListShopCartTemp.Count > 0)
                {
                    lbResulFindShoppingCart.Text = "";
                    lbResulFindShoppingCart.Visible = false;

                    Session["ShoppingCartTemp"] = ListShopCartTemp;
                    GridViewItem.DataSource = ListShopCartTemp;
                    GridViewItem.DataBind();
                    GridViewItem.Visible = true;
                    lbTotalOrder.Text = "Tổng hóa đơn: " + ShoppingCart.GetTotalShoppingCart(ListShopCartTemp).ToString() + " VNĐ";
                    lbTotalOrder.Visible = true;
                }
                else
                {
                    Session["ShoppingCartTemp"] = null;
                    GridViewItem.Visible = false;
                    lbTotalOrder.Visible = false;
                    lbResulFindShoppingCart.Text = "Không có thông tin giỏ hàng tạm lưu hoặc giỏ hàng tạm lưu của bạn quá thời hạn lưu trữ";
                    lbResulFindShoppingCart.Visible = true;
                }
            }
            else
            {
                Session["ShoppingCartTemp"] = null;
                GridViewItem.Visible = false;
                lbTotalOrder.Visible = false;
                lbResulFindShoppingCart.Text = "Không có thông tin giỏ hàng tạm lưu hoặc giỏ hàng tạm lưu của bạn quá thời hạn lưu trữ";
                lbResulFindShoppingCart.Visible = true;
            }
        }
        else
        {
            Session["ShoppingCartTemp"] = null;
            GridViewItem.Visible = false;
            lbTotalOrder.Visible = false;
            lbResulFindShoppingCart.Text = "Không có thông tin giỏ hàng tạm lưu hoặc giỏ hàng tạm lưu của bạn quá thời hạn lưu trữ";
            lbResulFindShoppingCart.Visible = true;
        }
    }
    protected void btnAddShoppingCart_Click(object sender, EventArgs e)
    {
        if (Session["ShoppingCartTemp"] != null)
        {
            List<ShoppingCart> ListShopCartTemp = new List<ShoppingCart>();
            ListShopCartTemp = (List<ShoppingCart>)Session["ShoppingCartTemp"];
            List<ShoppingCart> ListShopCart = new List<ShoppingCart>();
            if (Session["ShoppingCart"] == null)
            {
                foreach (ShoppingCart item in ListShopCartTemp)
                {
                    ListShopCart.Add(item);
                    Session["ShoppingCart"] = ListShopCart;
                }
            }
            else
            {
                ListShopCart = (List<ShoppingCart>)Session["ShoppingCart"];
                foreach (ShoppingCart item in ListShopCartTemp)
                {
                    //ListShopCart.Add(item);
                    Session["ShoppingCart"] = ShoppingCart.AddItemToShoppingCart(ListShopCart, item);
                }
            }

            BindData((List < ShoppingCart >)Session["ShoppingCart"]);
            this.UpdatePanelShoppingCart.Update();

            //set null kq tìm kiếm sau khi add 
            Session["ShoppingCartTemp"] = null;
            lbResulFindShoppingCart.Text = "";
            lbResulFindShoppingCart.Visible = false;
            GridViewItem.DataSource = null;
            GridViewItem.DataBind();
            GridViewItem.Visible = true;
            lbTotalOrder.Text = "";
            lbTotalOrder.Visible = false;
            //end set nul

            CloseDialog("FindOrderTemp");
            
            try
            {
                UpdatePanel updatepan = new UpdatePanel();
                updatepan = (UpdatePanel)Page.Master.FindControl("ContentDynamic").FindControl("UpdatePanel1").FindControl("ShoppingCartDetail1").FindControl("UpdatePanelOnShoppingCartDetails");
                updatepan.Update();

                //UpdatePanel updatepanCustommer = new UpdatePanel();
                //updatepanCustommer = (UpdatePanel)Page.Master.FindControl("ContentPlaceHolder1").FindControl("UpdatePanel1").FindControl("ProceedOrder1").FindControl("UpdatePanel1");
                //updatepanCustommer.Update();  
            }
            catch{ }
            
        }
    }
    private void CloseDialog(string dialogId)
    {
        string script = string.Format(@"closeDialog('{0}')", dialogId);
        ScriptManager.RegisterClientScriptBlock(Page, typeof(Usercontrol_ShoppingCart), Page.UniqueID, script, true);
    }

    protected void btnClose_Click(object sender, EventArgs e)
    {
        //set null kq tìm kiếm sau khi add 
        Session["ShoppingCartTemp"] = null;
        lbResulFindShoppingCart.Text = "";
        lbResulFindShoppingCart.Visible = false;
        GridViewItem.DataSource = null;
        GridViewItem.DataBind();
        GridViewItem.Visible = true;
        lbTotalOrder.Text = "";
        lbTotalOrder.Visible = false;
        //end set nul
        CloseDialog("FindOrderTemp");
    }
}